import { Schema, model, Types } from "mongoose";

export interface ImageDoc {
  _id: Types.ObjectId;
  storageKey: string;
  originalUrl?: string;
  width: number;
  height: number;
  fileSize: number;
  hash: string;
  thumbUrl?: string;
  exif?: Record<string, unknown>;
  categories: Types.ObjectId[];
  tags: Types.ObjectId[];
  createdAt: Date;
  updatedAt: Date;
}

const ImageSchema = new Schema<ImageDoc>(
  {
    storageKey: { type: String, required: true },
    originalUrl: { type: String },
    width: { type: Number, required: true },
    height: { type: Number, required: true },
    fileSize: { type: Number, required: true },
    hash: { type: String, required: true, unique: true, index: true },
    thumbUrl: { type: String },
    exif: { type: Schema.Types.Mixed },
    categories: [{ type: Schema.Types.ObjectId, ref: "Category", index: true }],
    tags: [{ type: Schema.Types.ObjectId, ref: "Tag", index: true }],
  },
  { timestamps: true }
);

ImageSchema.index({ createdAt: -1 });
ImageSchema.index({ tags: 1 });
ImageSchema.index({ categories: 1 });

export const ImageModel = model<ImageDoc>("Image", ImageSchema);
